<?php
/*
 * inc-editType.php
 * Retral/CSCD494 Winter 2009
 * Eastern Washington University
 *
 * File Description:
 * Edit the type/category of given question
 * 
 * Other:
 * 
 */

/* 
 * Set up $page_name
 * This name will be compared to PAGE_NAME in NAV_URLS table (stored in session) and verify permission for given user.
 */
 
$page_name = "Exams";
$page_name = strtoupper($page_name);

$pm = $_SESSION['pagePermissions'];

// Is user logged in, and has permissions?
if (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] !="" && $_SESSION['pagePermissions'][$page_name] == 1)
{
?>
	<link rel='stylesheet' href="css/preview.css" type="text/css" media="screen">
	<script type="text/javascript" language="javascript" src="script/editQuestion.js"></script>
	<link rel='stylesheet' 
	href="css/code-bright.css" type="text/css" media="screen">
	<form action="<?php echo $_SERVER['PHP_SELF'] . '?page=editType&id=' . $_REQUEST['id'] . '&ver=' . $_REQUEST['ver']; ?>&min=5" method="POST" enctype="multipart/form-data" >

	<?php
		//require('class.highlighter.php');

		$conn = oci_connect(ORA_CON_UN, ORA_CON_PW, ORA_CON_DB);
		$id = $_REQUEST['id'];
		$ver = $_REQUEST['ver'];
			
		if(isset($_POST['submit']))
		{
			
			if($_POST['typeSelect'] == $_POST['origType'] && $_POST['pointSelect'] == $_POST['origQuestionPoints'] && $_POST['questionName'] == $_POST['origTitle'])
			{
				print "<p><font color='#00FF00'>Question Type Successfully Changed! </font></p><br>";  
				print "<a href='?page=question&id=" . $id . "&ver=" . $ver . "&min=5'>Back to Edit/View Question</a><br>";
				return;
			}
			
			$max = 'points' . $_POST['typeSelect'];
			//echo $_POST[$max] . ' ' . $_POST['pointSelect']  . '<br>';
			if($_POST['pointSelect'] > $_POST[$max])
			{
				print "<p>An error has occurred because Javascript is off. Please turn it on and try again.</p><br>";
				print "<a href='?page=question&id=" . $id . "&ver=" . $ver . "&min=5'>Back to Edit/View Question</a><br>";
				return;
			}
			
			$query =  ' BEGIN ' .
					  '     UPDATE QUESTION ' .
					  '     SET question_type_code = :code ' .
					  '     WHERE question_id = :ID AND q_version = :version; ' .
					  '                                                        ' .
					  '     UPDATE QUESTION_CONTENT ' .
					  '     SET points = :points, question_title = :title ' .
					  '     WHERE question_content_id = :contentID; ' .
					  ' END; ';


			
			$stmt = oci_parse($conn, $query);
			oci_bind_by_name($stmt, ':code',  $_POST['typeSelect']);
			oci_bind_by_name($stmt, ':ID', $id);
			oci_bind_by_name($stmt, ':version', $ver);
			oci_bind_by_name($stmt, ':points', $_POST['pointSelect']);
			oci_bind_by_name($stmt, ':title', $_POST['questionName']);
			oci_bind_by_name($stmt, ':contentID', $_POST['contentID']);
			
			if(!oci_execute($stmt))
				print "<p>Could not find question with id: $id</p><br>";
			else
				print "<p><font color='#00FF00'>Question Type Successfully Changed! </font></p><br>";       
		}
		else
		{
			
			$currType = -1;
			$currPoints = 5;
			$content_id = 0;
			$questionTitle = '';
			
			$query = ' BEGIN ' .
					 '     SELECT question.question_type_code, question_content.points, ' .
					 '     question_content.question_content_id, question_content.question_title ' .
					 '     INTO :code, :points, :content_id, :title ' .
					 '     FROM QUESTION INNER JOIN QUESTION_CONTENT ' .
					 '     ON question.question_content_id = question_content.question_content_id  '.
					 '     WHERE question.question_id = :ID AND question.q_version = :version;' .
					 ' END; ';

			$id = $_REQUEST['id'];
			$ver = $_REQUEST['ver'];
			
			$stmt = oci_parse($conn, $query);
			oci_bind_by_name($stmt, ':code',  $currType, 64);
			oci_bind_by_name($stmt, ':points',  $currPoints, 64);
			oci_bind_by_name($stmt, ':content_id',  $content_id, 64);
			oci_bind_by_name($stmt, ':title',  $questionTitle, 64);
			oci_bind_by_name($stmt, ':ID', $id);
			oci_bind_by_name($stmt, ':version', $ver);
			
			if(!oci_execute($stmt))
				print "<P>Could not find question with id: $id";
			


			$query = ' SELECT * FROM QUESTION_TYPE ';
			$stmt = oci_parse($conn, $query);
			if(!oci_execute($stmt))
			{
				print "<br> Internal error occurred. Please contact the administrator";
				return;
			}
			
			$rows = 0;
			$description = array();
			$points = array();
			
			while($row = oci_fetch_assoc($stmt)) 
			{
				$description[$row['TYPE_CODE']] = $row['TYPE_DESCRIPTION'];
				$points[$row['TYPE_CODE']] = $row['POINTS'];
				$rows++;
			}
			if($rows == 0)
				echo "No types available at this time. Contact administrator.";
			else
			{
				print "<h3> Current Question Type: " . $description[$currType] . " - " . $currPoints . " points </h3><br>";
				print '<table><tr><td>';
				print 'Select question type: </td><td>' .
					  '<select name="typeSelect" id="typeSelect" onChange="changePoints(this.form, this)">';
				foreach($points as $index => $value)
				{
					print '<option value =' . $index;
					if($index == $currType)
						print ' selected="yes" '; 
					print  '>' . strtoupper($description[$index]);
					print ' - ' . $value. ' points</option>';
				}   
				print '</select> </td></tr>';
				
				print '<tr><td> Select Number of Points: </td><td>' .
					  '<select name="pointSelect" id="pointSelect">';
				for($x = 5; $x <= $points[$currType]; $x = $x + 5)
				{
					echo '<option value =' . $x;
					if($currPoints == $x)
						echo " selected='yes'"; 
					 echo  '>' . $x . '</option>';
				}   
						 
				print '</select></td></tr><tr><td>';
				print 'Enter question title: </td><td>' .
					  '<input type="text" name="questionName" value="' . $questionTitle .  '">';
				print '</td></tr></table>';
				print '<input type="submit" value="Go" name="submit" id="submit"> <br> ';
				print '<input type="hidden" value="' . $currPoints . '" name="origQuestionPoints"> <br>';
				print '<input type="hidden" value="' . $currType . '" name="origType"> ';
				print '<input type="hidden" value="' . $content_id . '" name="contentID">';
				print '<input type="hidden" value="' . $questionTitle . '" name="origTitle">';
				foreach($points as $index => $value)
				{
					//print "$index => $value";
					print '<input type="hidden" value="' . $value . '" name="points' . $index . '">';   
				}
			}
		}
		
		print "<a href='?page=question&id=" . $id . "&ver=" . $ver . "&min=5'>Back to Edit/View Question</a><br>";
		
	?>
	</form>
<?php
} //end if logged in
else
{
	echo "Invalid credentials";
	echo "<META http-equiv=\"refresh\" content=\"0;URL=index.php?page=login&ld=0\">";
}	
?>